package com.hxkj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.UserFootprintDao;
import com.hxkj.domain.enums.UserExceptionEnum;
import com.hxkj.domain.po.UserFootprint;
import com.hxkj.service.UserFootprintService;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import com.platform.common.service.sftp.domain.annotation.FormatUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * created by kezhijie on 2018/8/28 9:07.
 * email to kezhijie@hxoss.com
 */
@Service
public class UserFootprintServiceImpl implements UserFootprintService {

    @Autowired
    private UserFootprintDao userFootprintDao;

    @Override
    @FormatUrl
    @Transactional(rollbackFor = Exception.class)
    public ResultMessageStruct save(UserFootprint userFootprint) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();

        if (Objects.isNull(userFootprint) || Objects.isNull(userFootprint.getUserId())) {
            resultMessageStruct.setCode(UserExceptionEnum.USERINFO_INVALID.getCode());
            resultMessageStruct.setMessage(UserExceptionEnum.USERINFO_INVALID.getMsg());
            return resultMessageStruct;
        }

        int i = userFootprintDao.save(userFootprint);

        if (i > 0) {
            resultMessageStruct.setCode(200);
            resultMessageStruct.setMessage("成功");
        } else {
            resultMessageStruct.setCode(500);
            resultMessageStruct.setMessage("失败");
        }

        return resultMessageStruct;
    }

    @Override
    @FormatUrl(type = "after")
    public PageInfo selectListByUserId(UserFootprint userFootprint, PageTo pageParam) {
        if (Objects.isNull(pageParam.getPageNum()) || Objects.isNull(pageParam.getPageSize())) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }

        Page<UserFootprint> page = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<UserFootprint> list = userFootprintDao.selectList(userFootprint);
        PageInfo<UserFootprint> pageInfo = new PageInfo<>(page);

        return pageInfo;
    }
}
